package space.xxhui.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import space.xxhui.DAO.UserDao;
import space.xxhui.POJO.User;

@Controller
public class LoginController {

    @Autowired
    private UserDao userDao;

    //遇到根目录请求的时候跳转到Login.jsp页面
    @RequestMapping("/")
    public String gotoLogin(){
        return "Login";
    }

    /**
     * 1. 使用RequestMapping注解来映射请求的URL
     * 2. 返回值会通过视图解析器解析为实际的物理视图, 对于InternalResourceViewResolver视图解析器，会做如下解析
     * 通过prefix+returnVal+suffix 这样的方式得到实际的物理视图，然后会转发操作
     * "/WEB-INF/views/xxxxxx.jsp"
     * @return
     */
//    @RequestMapping("/login")
//    public String login(@RequestParam("username") String username , @RequestParam("pwd") String pwd){
//        String forword = null;
//        if (username.equals("Hitvz") && pwd.equals("123456")) {
//            forword = "index";//登录成功跳转到index.jsp
//        } else {
//            System.out.println("no such user");
//            forword = "Login";//登录失败跳转到Login.jsp继续登录操作
//        }
//        return forword;
//    }

    @RequestMapping("/login")
    public String login(@RequestParam("username") String username , @RequestParam("pwd") String pwd){
        String forword = null;
        User userEntity =new User();
        userEntity.setName(username);
        userEntity.setPwd(pwd);
        //mybatis配置文件中设置了，如果没有查询到返回null
        User entity = userDao.checkUser(userEntity);
        //if (username.equals("Hitvz") && pwd.equals("123456")) {
        if(entity!=null){
            forword = "index";//登录成功跳转到index.jsp
        } else {
            System.out.println("no such user");
            forword = "Login";//登录失败跳转到Login.jsp继续登录操作
        }
        return forword;
    }
}
